package cms.manage.template.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import cms.bean.follow.FollowEntity;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import cms.bean.follow.Follow;
import cms.bean.template.Forum;
import cms.bean.user.AccessUser;
import cms.bean.user.User;
import cms.service.follow.FollowService;
import cms.manage.FollowManage;
import cms.manage.FollowerManage;
import cms.manage.user.UserManage;

/**
 * 关注 -- 模板方法实现
 *
 */
@Component
public class FollowTemplateManage extends BaseTemplateManage{
	@Resource FollowService followService; 
	@Resource FollowManage followManage;
	@Resource FollowerManage followerManage;
	@Resource UserManage userManage;
	
	
	/**
	 * 关注总数  -- 实体对象
	 * @param forum 版块对象
	 * @param parameter 参数
	 */
	public Long followCountEntityBean(Forum forum, Map<String,Object> parameter, Map<String,Object> runtimeParameter){
		String userName = MapUtils.getString(parameter,"userName");
		if(userName != null && !"".equals(userName.trim())){
			User user = userManage.queryCacheFindUserByUserName(userName.trim());
			if(user != null){
				return followManage.query_cache_followCount(user.getId(), user.getUserName());
			}
		}
		return 0L;
	}
	
	/**
	 * 粉丝总数  -- 实体对象
	 * @param forum 版块对象
	 * @param parameter 参数
	 */
	public Long followerCountEntityBean(Forum forum, Map<String,Object> parameter, Map<String,Object> runtimeParameter){
		//获取参数
		String userName = MapUtils.getString(parameter,"userName");
		if(userName != null && !"".equals(userName.trim())){
			User user = userManage.queryCacheFindUserByUserName(userName.trim());
			if(user != null){
				return followerManage.query_cache_followerCount(user.getId(), user.getUserName());
			}
		}
		return 0L;
	}
	
	
	
	/**
	 * 是否已经关注该用户  -- 实体对象
	 * @param forum 版块对象
	 * @param parameter 参数
	 */
	public Boolean followingEntityBean(Forum forum, Map<String,Object> parameter, Map<String,Object> runtimeParameter){
		//获取参数
		String userName = MapUtils.getString(parameter,"userName");
		AccessUser accessUser = getAccessUser(runtimeParameter);
		if(accessUser != null && userName != null && !"".equals(userName.trim())){
			User user = userManage.queryCacheFindUserByUserName(userName.trim());
			if(user != null){
				//生成关注Id
				String followId = followManage.createFollowId(accessUser.getUserId(),user.getId());
				FollowEntity follow = followManage.queryCacheFindById(followId);
				if(follow != null){
		  			return true;
			  	}
			}
		}
		return false;
	}
	
	
	/**
	 * 关注用户
	 * @param forum
	 */
	public Map<String,Object> addFollowCollection(Forum forum, Map<String,Object> parameter, Map<String,Object> runtimeParameter){
		Map<String,Object> value = new HashMap<String,Object>();
		
		return value;
	}
}
